﻿using Microsoft.AspNetCore.Http;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.DataContract.Result;
using ThresholdAssistant.Http.Extensions;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace ThresholdAssistant.Http
{
    /// <summary>
    /// Http客户端请求帮助类（透传Header头部信息）
    /// </summary>
    public class HttpClientHeaderUtil : IBaseAutofac
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IHttpClientFactory factory;

        /// <summary>
        /// Http上下文
        /// </summary>
        private readonly IHttpContextAccessor _httpContext;

        /// <summary>
        /// 头部信息
        /// </summary>
        private readonly Dictionary<string, string> _headers;

        /// <summary>
        /// 注入构造函数
        /// </summary>
        /// <param name="_actory"></param>
        /// <param name="httpContext"></param>
        public HttpClientHeaderUtil(IHttpClientFactory _actory, IHttpContextAccessor httpContext)
        {
            factory = _actory;
            _httpContext = httpContext;
            _headers = _httpContext.ToHeaders();//转换头部信息
        }

        /// <summary>
        /// Get接口请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult> GetAsync(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            headers = headers.AddHeaders(_headers);
            return await factory.GetAsync<ContractResult>(url, paramters, headers, timeoutSeconds);
        }

        /// <summary>
        /// Get接口请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult<T>> GetAsync<T>(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            headers = headers.AddHeaders(_headers);
            return await factory.GetAsync<ContractResult<T>>(url, paramters, headers, timeoutSeconds);
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R">请求模型</typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType">协议类型</param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult> PostAsync<R>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            headers = headers.AddHeaders(_headers);
            return await factory.PostAsync<R, ContractResult>(url, paramters, headers, contentType, timeoutSeconds);
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R">请求模型</typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType">协议类型</param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult<T>> PostAsync<R, T>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            headers = headers.AddHeaders(_headers);
            return await factory.PostAsync<R, ContractResult<T>>(url, paramters, headers, contentType, timeoutSeconds);
        }
    }
}